#pragma once

#include <QtWidgets/QMainWindow>
#include "hv/HttpServer.h"
#include "hv/WebSocketServer.h"
#include <hv/WebSocketChannel.h>
#include <json/json.h>

enum class cmd {
    INITCON,
    SEND,
    SENDIMG
};

enum class httpCmd {
    H_GET, 
    H_POST, 
    H_PUT,
    H_DELETE,
    H_PATCH, 
    H_HEAD
};

class QLabel;
class QLineEdit;
class QPushButton;
class QTextEdit;
class TWebSocketServer : public QMainWindow
{
    Q_OBJECT

public:
    TWebSocketServer(QWidget *parent = nullptr);
    ~TWebSocketServer();

private:
    void initUI();
    bool stringToImage(const std::string& base64Image, const std::string& name);
    void initConnect();
    bool start(int port, const char* host = "0.0.0.0");
    void stop();
    cmd intTocmd(int i);
    httpCmd httpCmdToInt(int);
    void initClientInfo(const std::string& id, const WebSocketChannelPtr& channel);
    void sendMsg(const std::string& content);
    void saveImage(const std::string& image, const std::string& imageName);


    QLabel* m_pHost;
    QLabel* m_pIp;
    QLineEdit* m_pHostLineEdit;
    QLineEdit* m_pIpLineEdit;
    QPushButton* m_pStartButton;
    QPushButton* m_pCloseButton;
    QTextEdit* m_pTextEdit;
    QTextEdit* m_pDisplayTextEdit;

    hv::WebSocketServer* server;
    hv::HttpService* service;
    hv::WebSocketService* ws;

    std::unordered_map<std::string, WebSocketChannelPtr> clientMap;
};

